home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / dsiic2.zip / L_TRIM.C < prev    next >
C/C++ Source or Header  |  1991-07-15  |  2KB  |  98 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************   L_TRIM.C   ***************************/
  4.  
  5. #include "mydef.h"
  6. #include <string.h>
  7.  
  8. /*****************************************************************
  9.  
  10.  Usage: void make_string(char *string,char letter,int count);
  11.  
  12.   char *string = pointer to string to build.
  13.   char letter  = letter to replicate.
  14.   int count    = length of string.
  15.  
  16.  
  17.   Builds a string consisting only of the character specified
  18.   by "ch".  The string is the length specified by "count".
  19.  
  20.   Example: make_string (string,'x',10);
  21.            results: string = "xxxxxxxxxx"
  22.  
  23. *****************************************************************/
  24.  
  25. void make_string(char *string,char letter,int count)
  26. {
  27. int j;
  28. for (j=0;j<count;j++) string[j]=letter;
  29. string[j]='\0';
  30. }
  31.  
  32.  
  33. /*****************************************************************
  34.  
  35.  Usage: void trim_left(char *string)
  36.  
  37.   char *string = string to strip of left spaces.
  38.  
  39.   Removes all the leftmost spaces from the string specified by
  40.   "string".
  41.  
  42. *****************************************************************/
  43.  
  44. void trim_left(char *string)
  45. {
  46. int i=0;
  47. int j=0;
  48.  
  49. if (!strlen(string)) return;
  50.  
  51.  while(string[i]== ' ' && string[i] != '\0') i++;
  52.  
  53.  while (string[i] != '\0')
  54.   string[j++]= string[i++];
  55.  
  56.  string[j]='\0';
  57. }
  58.  
  59.  
  60. /*****************************************************************
  61.  
  62.  Usage: void trim_right(char *string);
  63.  
  64.   char *string = string to strip of right spaces.
  65.  
  66.   Removes all the rightmost spaces from the string specified by
  67.   "string".
  68.  
  69. *****************************************************************/
  70.  
  71. void trim_right(char *string)
  72. {
  73. int pos;
  74. pos = strlen(string)-1;
  75.   while(string[pos]==' '){
  76.    string[pos--]= '\0';
  77.    if (pos < 0) break;
  78.   }
  79. }
  80.  
  81.  
  82. /*****************************************************************
  83.  
  84.  Usage: void trim(char *string)
  85.  
  86.   char *string = string to strip of left and right spaces.
  87.  
  88.   Removes all the leftmost and rightmost spaces from the string
  89.   specified by "string".
  90.  
  91. *****************************************************************/
  92.  
  93. void trim(char *string)
  94. {
  95. trim_left(string);
  96. trim_right(string);
  97. }
  98.